package com.example.myapplication2;

import android.app.AlertDialog;
import android.app.Presentation;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;
import android.net.Uri;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class MyPresentation extends Presentation {
    private Context context;
    private Display display;
    private ImageView imageView;
    private int currentResId = -1; // 记录当前加载的资源ID

    public MyPresentation(Context outerContext, Display display) {
        super(outerContext, display);
        this.context = outerContext;
        this.display = display;
        if (getWindow() != null) {
            if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
                getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
            }else{
                getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_presentation);

        //初始化image
        imageView = findViewById(R.id.presentation_image);
        //loadImage();
        initView();
    }

    private void initView() {
        findViewById(R.id.show_dialog).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(context)
                        .setTitle("提示")
                        .setMessage("副屏触发的对话框")
                        .setCancelable(false)
                        .setPositiveButton("OK", new OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(getContext(), "主屏对话框被点击", Toast.LENGTH_SHORT).show();

                            }
                        }).create().show();
            }
        });
        findViewById(R.id.dismiss).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    private void loadImage(){
        imageView.setImageResource(R.drawable.www001);
    }

    // 新增方法：设置 drawable 资源
    public void setImageResource(int resId) {
        currentResId = resId;
        imageView.setImageResource(resId);
    }

    // 新增方法：设置相册图片 URI
    public void setImageUri(Uri uri) {
        try {
            // 使用 ContentResolver 加载图片
            InputStream inputStream = getContext().getContentResolver().openInputStream(uri);
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            imageView.setImageBitmap(bitmap);
        } catch (IOException e) {
            Log.e("MyPresentation", "图片加载失败: " + e.getMessage());
        }
    }
}
